﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.SettlementInfoModel;
using System.Linq;
using FwProject.Core.Distribution;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;
using FwProject.PublicService.JPush;
using FwProject.Authorization.Users;
using FwProject.Core.Restaurant;
using FwProject.JPush.JPushModel;

namespace FwProject.BackGround.DistributionEnter
{
    public class DistributionEnterPushMessageWorkBackGround : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        private readonly IRepository<DistributionInfo, Guid> _repositoryDistributionInfo;
        private readonly IRepository<User, long> _userRepository;
        public readonly IJPushAppservice _jPushAppservice;
        public readonly IRepository<RestaurantUserInfo, Guid> _repositoryRestaurantUserInfo;
        public ILogger logger { get; set; }
        public DistributionEnterPushMessageWorkBackGround(AbpTimer timer, IRepository<DistributionInfo, Guid> repositoryDistributionInfo, 
            IJPushAppservice jPushAppservice,
            IRepository<User, long> userRepository,
            IRepository<RestaurantUserInfo, Guid> repositoryRestaurantUserInfo) :base(timer) {

#if DEBUG
            Timer.Period = 1000 * 60;
#else
           Timer.Period = 1000 * 60 * 60 * 24;
#endif
            _repositoryDistributionInfo = repositoryDistributionInfo;
             logger = NullLogger.Instance;
            _jPushAppservice = jPushAppservice;
            _userRepository = userRepository;
            _repositoryRestaurantUserInfo = repositoryRestaurantUserInfo;
        }
        [UnitOfWork]
        protected override void DoWork()
        {
            try
            {
                using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
                {
                    logger.Info("后台任务开始提醒配送确认");
                    var t = _repositoryDistributionInfo.GetAll().Where(o => o.Pricestatus == Pricestatus.已配送 && o.CreationTime.AddDays(5) <= DateTime.Now).Select(s=>s.Restaurantid);
                    logger.Info($"后台任务开始提醒配送确认{t.Count()}需要处理");
                    foreach (var item in t)
                    {
                        var model = _repositoryRestaurantUserInfo.GetAllIncluding(o => o.EmployeeRestauranUser).Where(o => o.RestaurantInfoId == item).FirstOrDefault();
                        DistributionPushMessageModel pushModel = new DistributionPushMessageModel();
                        pushModel.TopicModel = JPush.JPushHelpel.TopicRestauran;
                        pushModel.UserId = model.EmployeeRestauranUser.Id;
                        pushModel.Id = model.Id;
                        pushModel.RestaurantId = item.Value;
                        _jPushAppservice.RestaurantPushMessage(model.EmployeeRestauranUser.Mobile, pushModel, $"您有未确认配送的餐具待确认，系统将在2天后自动确人收货，请查看详情");

                    }
                    logger.Info($"后台任务开始提醒配送确认完成");

                    CurrentUnitOfWork.SaveChanges();
                }
            }
            catch (Exception exe)
            {

                logger.Error("后台任务开始处理配送处理失败", exe);
            }
           
        }
    }
}
